# checkout_repos.py
import json, subprocess, os
from tqdm import tqdm

os.makedirs("repos", exist_ok=True)

repos = set()
# ✅ 加 encoding='utf-8'
with open("swebench/test.jsonl", encoding="utf-8") as f:
    for line in f:
        ins = json.loads(line)
        repos.add((ins["repo"], ins["base_commit"]))

for repo, commit in tqdm(repos, desc="checkout"):
    folder = f"repos/{repo.replace('/', '@')}"
    print(f"git clone https://github.com/{repo}.git {folder}")
    if not os.path.exists(folder):
        # ✅ 把 clone 地址空格去掉（你原来多一个空格）
        subprocess.run(
            f"git clone https://github.com/{repo}.git {folder}",
            shell=True, check=True
        )
    subprocess.run(f"cd {folder} && git checkout {commit}", shell=True, check=True)